package com.dex4d.ui.wallet.activity;

import android.widget.EditText;

import com.dex4d.R;
import com.dex4d.core.AppToolBarBaseActivity;
import com.dex4d.dialog.CommonMessageDialog;
import com.dex4d.dialog.CustomDialog;
import com.dex4d.event.ExitEvent;
import com.dex4d.model.User;
import com.dex4d.utils.Commons;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by chengyuchun on 2018/11/1.
 */
public class InvitationActivity extends AppToolBarBaseActivity implements Commons.BUNDLE_KEYS{

    @BindView(R.id.et_code)
    EditText mEtCode;

    @Override
    protected void initToolbar() {
        showBackView();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_invitation;
    }

    @Override
    protected void initVariables() {
        EventBus.getDefault().register(this);
    }

    @Override
    protected void loadData() {

    }

    @OnClick(R.id.btn_confirm)
    public void onViewClick(){
        if(mEtCode.getText().length()==0){
            CommonMessageDialog dialog = new CommonMessageDialog(this,CommonMessageDialog.DialogStyle.TIP);
            dialog.setMsg(getString(R.string.input_referral));
            dialog.setLeftBtnTxt(getString(R.string.ok));
            dialog.setClickListener(new CustomDialog.CustomDialogOnClickListener() {
                @Override
                public void click(boolean flag) {
                    if (!flag) {
                    }
                }
            });
            dialog.show();
        }else {
            //todo 设置别人的推荐地址,需要调用接口
            User.getInstance().setReferralAddress(mEtCode.getText().toString());
            User.getInstance().save();

            String target = getIntent().getStringExtra(Commons.BUNDLE_KEYS.TO_TARGET);
            if(TO_RESTORE.equals(target)){
                //导入
                openActivity(RestoreActivity.class);
            }else {
                //创建钱包
                openActivity(GenerateMnemonicActivity.class);
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(ExitEvent event) {
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
